package com.bookworld.model;

import jakarta.validation.constraints.*;
import org.hibernate.validator.constraints.URL;

import java.time.LocalDateTime;
import java.util.List;

public class Category {
    private Long id;
    
    @NotBlank(message = "{category.name.notblank}")
    @Size(max = 100, message = "{category.name.size}")
    private String name;
    
    @NotBlank(message = "{category.slug.notblank}")
    @Size(max = 100, message = "{category.slug.size}")
    @Pattern(regexp = "^[a-z0-9-]+$", message = "{category.slug.invalid}")
    private String slug;
    
    @Size(max = 500, message = "{category.description.size}")
    private String description;
    
    @URL(message = "{category.imageUrl.invalid}")
    private String imageUrl;
    
    private Long parentId;
    
    private Integer sortOrder;
    
    @NotNull(message = "{category.active.notnull}")
    private Boolean active;
    
    private String metaTitle;
    
    @Size(max = 500, message = "{category.metaDescription.size}")
    private String metaDescription;
    
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    
    // 关联对象
    private Category parent;
    private List<Category> children;
    private List<Book> books;

    public Category() {
        this.active = true;
        this.sortOrder = 0;
        this.createdAt = LocalDateTime.now();
    }

    // 获取完整路径名称
    public String getFullPathName() {
        if (parent != null) {
            return parent.getFullPathName() + " > " + name;
        }
        return name;
    }

    // 获取层级深度
    public int getDepth() {
        if (parent != null) {
            return parent.getDepth() + 1;
        }
        return 0;
    }

    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSlug() {
        return slug;
    }

    public void setSlug(String slug) {
        this.slug = slug;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public Integer getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(Integer sortOrder) {
        this.sortOrder = sortOrder;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public String getMetaTitle() {
        return metaTitle;
    }

    public void setMetaTitle(String metaTitle) {
        this.metaTitle = metaTitle;
    }

    public String getMetaDescription() {
        return metaDescription;
    }

    public void setMetaDescription(String metaDescription) {
        this.metaDescription = metaDescription;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Category getParent() {
        return parent;
    }

    public void setParent(Category parent) {
        this.parent = parent;
    }

    public List<Category> getChildren() {
        return children;
    }

    public void setChildren(List<Category> children) {
        this.children = children;
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    @Override
    public String toString() {
        return "Category{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", slug='" + slug + '\'' +
                ", active=" + active +
                '}';
    }
}
